
#include <asm.h>

.global C_SYMBOL(DrawXMajorLPEdge)
.global C_SYMBOL(DrawXMajorLNEdge)
.global C_SYMBOL(DrawYMajorLEdge)

.global C_SYMBOL(DrawXMajorRPEdge)
.global C_SYMBOL(DrawXMajorRNEdge)
.global C_SYMBOL(DrawYMajorREdge)

C_SYMBOL(DrawVerticalEdge):
    STMFD	SP!, {R4-R12,LR}
Start1:
End1:
	LDMFD	SP!, {R4-R12,PC}




C_SYMBOL(DrawHorizontalEdge):
    STMFD	SP!, {R4-R12,LR}
Start2:
End2:
	LDMFD	SP!, {R4-R12,PC}

C_SYMBOL(DrawXMajorLPEdge):

    STMFD	SP!, {R4-R12,LR}
StartXLP:
    MOV     R3,  R0
    LDR     R12, [R3,#0x0]       //pDst
    LDR     R5,  [R3,#0x4]       //nPitch
    LDR     R6,  [R3,#0x8]       //startP
    LDR     R10, [R3,#0xC]       //deltaD
    LDR     R7,  [R3,#0x10]      //s16
    LDR     R8,  [R3,#0x14]      //d16
    LDR     R11, [R3,#0x18]      //inc
    LDR     R0,  [R3,#0x20]      //pSrc
    LDR     R9,  [R3,#0x24]      //scanlinelist
    LDR     LR,  [R3,#0x28]      //startY

    TMCRR   wR13 , R9, R9
    WUNPCKELUB wR13, wR13

    LDR	   R2, =0x00000008
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00FF00FF
    TMCRR   wR14, R2, R2

    WZERO   wR12


XLPLoop:

    MOV     R2, R7, ASR #16
    ADD     R2, R2, #1
    MLA     R4, R2, R5, R12
    MLA     R3, R2, R5, R0

    ADD     R4, R4, R6, LSL# 2      //dst
    ADD     R3, R3, R6, LSL# 2      //src

    WLDRW   wR0, [R3]               //src color
    WLDRW   wR1, [R4]               //dst color
    TEXTRMUB R1, wR6, #3
    TBCSTH  wR4, R1                 //wR4 = srcA
    WUNPCKELUB wR1, wR1
    MOV     R1, R7, LSR #8
    TBCSTB  wR5, R1
    WAND    wR5, wR5, wR14          //wR5 = Alpha
    WMULUL  wR4, wR4, wR5
    WUNPCKELUB wR0, wR0
    WSRLH   wR4, wR4, wR15          //wR4 = srcA*A
    WMULUL  wR0, wR0, wR5
    WSUBH   wR4, wR14, wR4
    WSRLH   wR0, wR0, wR15
    WMULUL  wR1, wR1, wR4
    WSRLH   wR1, wR1, wR15
    WADDH   wR0, wR1, wR0
    WPACKHUS wR0, wR0, wR12
    WSTRW   wR0, [R4]

    SUB R2, R2, LR
    ADD R1, R9, R2, LSL #4

    ADD R2, R6, #1
    STR R2, [R1]

    SUBS    R10, R10, #1

    ADD     R7, R7, R8
    ADD     R6, R6, R11
    BGT     XLPLoop

EndXLP:
	LDMFD	SP!, {R4-R12,PC}



C_SYMBOL(DrawXMajorLNEdge):

    STMFD	SP!, {R4-R12,LR}
StartXLN:
    MOV     R3,  R0
    LDR     R12, [R3,#0x0]       //pDst
    LDR     R5,  [R3,#0x4]       //nPitch
    LDR     R6,  [R3,#0x8]       //startP
    LDR     R10, [R3,#0xC]       //deltaD
    LDR     R7,  [R3,#0x10]      //s16
    LDR     R8,  [R3,#0x14]      //d16
    LDR     R11, [R3,#0x18]      //inc
    LDR     R0,  [R3,#0x20]      //pSrc
    LDR     R9,  [R3,#0x24]      //scanlinelist
    LDR     LR,  [R3,#0x28]      //startY

    TMCRR   wR13 , R9, R9
    WUNPCKELUB wR13, wR13

    LDR	   R2, =0x00000008
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00FF00FF
    TMCRR   wR14, R2, R2

    WZERO   wR12


XLNLoop:

    MOV     R2, R7, ASR #16
//    ADD     R2, R2, #1
    MLA     R4, R2, R5, R12
    MLA     R3, R2, R5, R0

    ADD     R4, R4, R6, LSL# 2      //dst
    ADD     R3, R3, R6, LSL# 2      //src

    WLDRW   wR0, [R3]               //src color
    WLDRW   wR1, [R4]               //dst color
    TEXTRMUB R1, wR6, #3
    TBCSTH  wR4, R1                 //wR4 = srcA
    WUNPCKELUB wR1, wR1
    MOV     R1, R7, LSR #8
    TBCSTB  wR5, R1
    WAND    wR5, wR5, wR14          //wR5 = Alpha
    WSUBH   wR5, wR14, wR5
    WMULUL  wR4, wR4, wR5
    WUNPCKELUB wR0, wR0
    WSRLH   wR4, wR4, wR15          //wR4 = srcA*A
    WMULUL  wR0, wR0, wR5
    WSUBH   wR4, wR14, wR4
    WSRLH   wR0, wR0, wR15
    WMULUL  wR1, wR1, wR4
    WSRLH   wR1, wR1, wR15
    WADDH   wR0, wR1, wR0
    WPACKHUS wR0, wR0, wR12
    WSTRW   wR0, [R4]

    ADD     R2, R2, #1
    SUB R2, R2, LR
    ADD R1, R9, R2, LSL #4

//    ADD R2, R6, #1
    STR R6, [R1]

    SUBS    R10, R10, #1

    ADD     R7, R7, R8
    ADD     R6, R6, R11
    BGT     XLNLoop

EndXLN:
	LDMFD	SP!, {R4-R12,PC}


C_SYMBOL(DrawYMajorLEdge):

    STMFD	SP!, {R4-R12,LR}
StartYL:
    MOV     R3,  R0
    LDR     R12, [R3,#0x0]       //pDst
    LDR     R5,  [R3,#0x4]       //nPitch
    LDR     R6,  [R3,#0x8]       //startP Y
    LDR     R10, [R3,#0xC]       //deltaD Dy
    LDR     R7,  [R3,#0x10]      //s16      x
    LDR     R8,  [R3,#0x14]      //d16
    LDR     R11, [R3,#0x18]      //inc      yinc
    LDR     R0,  [R3,#0x20]      //pSrc
    LDR     R9,  [R3,#0x24]      //scanlinelist
    LDR     LR,  [R3,#0x28]      //startY

    TMCRR   wR13 , R9, R9
    WUNPCKELUB wR13, wR13

    LDR	   R2, =0x00000008
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00FF00FF
    TMCRR   wR14, R2, R2

    WZERO   wR12


YLLoop:

    MOV     R2, R7, ASR #16

    MLA     R4, R6, R5, R12
    MLA     R3, R6, R5, R0

    ADD     R4, R4, R2, LSL# 2      //dst
    ADD     R3, R3, R2, LSL# 2      //src

    WLDRW   wR0, [R3]               //src color
    WLDRW   wR1, [R4]               //dst color
    TEXTRMUB R1, wR6, #3
    TBCSTH  wR4, R1                 //wR4 = srcA
    WUNPCKELUB wR1, wR1
    MOV     R1, R7, LSR #8
    TBCSTB  wR5, R1
    WAND    wR5, wR5, wR14          //wR5 = Alpha
    WSUBH   wR5, wR14, wR5
    WMULUL  wR4, wR4, wR5
    WUNPCKELUB wR0, wR0
    WSRLH   wR4, wR4, wR15          //wR4 = srcA*A
    WMULUL  wR0, wR0, wR5
    WSUBH   wR4, wR14, wR4
    WSRLH   wR0, wR0, wR15
    WMULUL  wR1, wR1, wR4
    WSRLH   wR1, wR1, wR15
    WADDH   wR0, wR1, wR0
    WPACKHUS wR0, wR0, wR12
    WSTRW   wR0, [R4]

//    ADD     R2, R2, #1
    SUB R3, R6, LR
    ADD R1, R9, R3, LSL #4

    ADD R2, R2, #1
    STR R2, [R1]

    SUBS    R10, R10, #1

    ADD     R7, R7, R8
    ADD     R6, R6, R11
    BGT     YLLoop

EndYL:
	LDMFD	SP!, {R4-R12,PC}



C_SYMBOL(DrawXMajorRPEdge):

    STMFD	SP!, {R4-R12,LR}
StartXRP:
    MOV     R3,  R0
    LDR     R12, [R3,#0x0]       //pDst
    LDR     R5,  [R3,#0x4]       //nPitch
    LDR     R6,  [R3,#0x8]       //startP
    LDR     R10, [R3,#0xC]       //deltaD
    LDR     R7,  [R3,#0x10]      //s16
    LDR     R8,  [R3,#0x14]      //d16
    LDR     R11, [R3,#0x18]      //inc
    LDR     R0,  [R3,#0x20]      //pSrc
    LDR     R9,  [R3,#0x24]      //scanlinelist
    LDR     LR,  [R3,#0x28]      //startY

    TMCRR   wR13 , R9, R9
    WUNPCKELUB wR13, wR13

    LDR	   R2, =0x00000008
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00FF00FF
    TMCRR   wR14, R2, R2

    WZERO   wR12


XRPLoop:

    MOV     R2, R7, ASR #16
//    ADD     R2, R2, #1
    MLA     R4, R2, R5, R12
    MLA     R3, R2, R5, R0

    ADD     R4, R4, R6, LSL# 2      //dst
    ADD     R3, R3, R6, LSL# 2      //src

    WLDRW   wR0, [R3]               //src color
    WLDRW   wR1, [R4]               //dst color
    TEXTRMUB R1, wR6, #3
    TBCSTH  wR4, R1                 //wR4 = srcA
    WUNPCKELUB wR1, wR1
    MOV     R1, R7, LSR #8
    TBCSTB  wR5, R1
    WAND    wR5, wR5, wR14          //wR5 = Alpha
    WSUBH   wR5, wR14, wR5
    WMULUL  wR4, wR4, wR5
    WUNPCKELUB wR0, wR0
    WSRLH   wR4, wR4, wR15          //wR4 = srcA*A
    WMULUL  wR0, wR0, wR5
    WSUBH   wR4, wR14, wR4
    WSRLH   wR0, wR0, wR15
    WMULUL  wR1, wR1, wR4
    WSRLH   wR1, wR1, wR15
    WADDH   wR0, wR1, wR0
    WPACKHUS wR0, wR0, wR12
    WSTRW   wR0, [R4]

    ADD     R2, R2, #1
    SUB R2, R2, LR
    ADD R1, R9, R2, LSL #4

//    ADD R2, R6, #1
    STR R6, [R1, #8]

    SUBS    R10, R10, #1

    ADD     R7, R7, R8
    ADD     R6, R6, R11
    BGT     XRPLoop

EndXRP:
	LDMFD	SP!, {R4-R12,PC}



C_SYMBOL(DrawXMajorRNEdge):

    STMFD	SP!, {R4-R12,LR}
StartXRN:
    MOV     R3,  R0
    LDR     R12, [R3,#0x0]       //pDst
    LDR     R5,  [R3,#0x4]       //nPitch
    LDR     R6,  [R3,#0x8]       //startP
    LDR     R10, [R3,#0xC]       //deltaD
    LDR     R7,  [R3,#0x10]      //s16
    LDR     R8,  [R3,#0x14]      //d16
    LDR     R11, [R3,#0x18]      //inc
    LDR     R0,  [R3,#0x20]      //pSrc
    LDR     R9,  [R3,#0x24]      //scanlinelist
    LDR     LR,  [R3,#0x28]      //startY

    TMCRR   wR13 , R9, R9
    WUNPCKELUB wR13, wR13

    LDR	   R2, =0x00000008
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00FF00FF
    TMCRR   wR14, R2, R2

    WZERO   wR12


XRNLoop:

    MOV     R2, R7, ASR #16
    ADD     R2, R2, #1
    MLA     R4, R2, R5, R12
    MLA     R3, R2, R5, R0
    ADD     R1, R6, #1
    ADD     R4, R4, R1, LSL# 2      //dst
    ADD     R3, R3, R1, LSL# 2      //src

    WLDRW   wR0, [R3]               //src color
    WLDRW   wR1, [R4]               //dst color
    TEXTRMUB R1, wR6, #3
    TBCSTH  wR4, R1                 //wR4 = srcA
    WUNPCKELUB wR1, wR1
    MOV     R1, R7, LSR #8
    TBCSTB  wR5, R1
    WAND    wR5, wR5, wR14          //wR5 = Alpha
    WMULUL  wR4, wR4, wR5
    WUNPCKELUB wR0, wR0
    WSRLH   wR4, wR4, wR15          //wR4 = srcA*A
    WMULUL  wR0, wR0, wR5
    WSUBH   wR4, wR14, wR4
    WSRLH   wR0, wR0, wR15
    WMULUL  wR1, wR1, wR4
    WSRLH   wR1, wR1, wR15
    WADDH   wR0, wR1, wR0
    WPACKHUS wR0, wR0, wR12
    WSTRW   wR0, [R4]

    SUB R2, R2, LR
    ADD R1, R9, R2, LSL #4

//    ADD R2, R6, #1
    STR R6, [R1, #8]

    SUBS    R10, R10, #1

    ADD     R7, R7, R8
    ADD     R6, R6, R11
    BGT     XRNLoop

EndXRN:
	LDMFD	SP!, {R4-R12,PC}


C_SYMBOL(DrawYMajorREdge):

    STMFD	SP!, {R4-R12,LR}
StartYR:
    MOV     R3,  R0
    LDR     R12, [R3,#0x0]       //pDst
    LDR     R5,  [R3,#0x4]       //nPitch
    LDR     R6,  [R3,#0x8]       //startP Y
    LDR     R10, [R3,#0xC]       //deltaD Dy
    LDR     R7,  [R3,#0x10]      //s16      x
    LDR     R8,  [R3,#0x14]      //d16
    LDR     R11, [R3,#0x18]      //inc      yinc
    LDR     R0,  [R3,#0x20]      //pSrc
    LDR     R9,  [R3,#0x24]      //scanlinelist
    LDR     LR,  [R3,#0x28]      //startY

    TMCRR   wR13 , R9, R9
    WUNPCKELUB wR13, wR13

    LDR	   R2, =0x00000008
    TMCRR   wR15 , R2, R2

    LDR     R2, = 0x00FF00FF
    TMCRR   wR14, R2, R2

    WZERO   wR12


YRLoop:

    MOV     R2, R7, ASR #16
    ADD     R2, R2, #1

    MLA     R4, R6, R5, R12
    MLA     R3, R6, R5, R0

    ADD     R4, R4, R2, LSL# 2      //dst
    ADD     R3, R3, R2, LSL# 2      //src

    WLDRW   wR0, [R3]               //src color
    WLDRW   wR1, [R4]               //dst color
    TEXTRMUB R1, wR6, #3
    TBCSTH  wR4, R1                 //wR4 = srcA
    WUNPCKELUB wR1, wR1
    MOV     R1, R7, LSR #8
    TBCSTB  wR5, R1
    WAND    wR5, wR5, wR14          //wR5 = Alpha
    WMULUL  wR4, wR4, wR5
    WUNPCKELUB wR0, wR0
    WSRLH   wR4, wR4, wR15          //wR4 = srcA*A
    WMULUL  wR0, wR0, wR5
    WSUBH   wR4, wR14, wR4
    WSRLH   wR0, wR0, wR15
    WMULUL  wR1, wR1, wR4
    WSRLH   wR1, wR1, wR15
    WADDH   wR0, wR1, wR0
    WPACKHUS wR0, wR0, wR12
    WSTRW   wR0, [R4]

//    ADD     R2, R2, #1
    SUB R3, R6, LR
    ADD R1, R9, R3, LSL #4

    SUB R2, R2, #1
    STR R2, [R1, #8]

    SUBS    R10, R10, #1

    ADD     R7, R7, R8
    ADD     R6, R6, R11
    BGT     YRLoop

EndYR:
	LDMFD	SP!, {R4-R12,PC}
